/*
Universal Molotilka 1.0
Copyright (c) 2011, 2012, Alexey Silaev ArchFurry@gmail.com
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
		list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
		this list of conditions and the following disclaimer in the documentation
		and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define SAFE_FILE_ENC 1 // 8 random bytes at start of file
#define OLD_FILE_ENC 0 // old style

struct UMcipher_context{
	size_t block_size; // 32-bit blocks
	uint32_t key;
	uint32_t sub_key;
	uint8_t *table; // 64k table
	uint8_t flag;
};

struct UMstream_context{
	uint32_t enc;
	uint32_t dec;
	uint32_t key;
	uint32_t sub_key;
	uint8_t *table;
};

struct UMhash_context{
	uint8_t *table;
	uint32_t hash[8];
	uint8_t hash_len;
	uint8_t ent_num;
};

void UMcipher_init
 (size_t block_size, uint32_t key, uint32_t sub_key,
  uint8_t *key_table, struct UMcipher_context *umc);
  
void UMcipher_simple_init_key_file
 (size_t block_size, char *password,
  char *key_file_name, struct UMcipher_context *umc);
  
void UMcipher_simple_init_token
 (size_t block_size, char *password,
  uint32_t *token, struct UMcipher_context *umc);

uint32_t UMcipher_modify
 (uint32_t a, uint32_t b, struct UMcipher_context *umc);
 
void UMcipher_block_encipher
 (uint32_t *block, struct UMcipher_context *umc);
 
void UMcipher_block_decipher
 (uint32_t *block, struct UMcipher_context *umc);
 
void UMcipher_file_encipher
 (char *input_file_name, char *output_file_name, struct UMcipher_context *umc);
 
void UMcipher_file_decipher
 (char *input_file_name, char *output_file_name, struct UMcipher_context *umc);

uint32_t UMstream_modify
 (uint32_t a, uint32_t b, struct UMstream_context *ums);

void UMstream_simple_init_key_file
 (uint8_t *password, uint8_t *key_file_name, struct UMstream_context *ums);

void UMstream_simple_init_token
 (uint8_t *password, uint32_t *token, struct UMstream_context *ums);

void UMstream_init
 (uint8_t *key_table, uint32_t key, uint32_t sub_key, struct UMstream_context *ums);

void UMstream_next_key
 (struct UMstream_context *ums);

void UMstream_next_sub_key
 (struct UMstream_context *ums);

uint32_t UMstream_enc_step
 (uint32_t next, struct UMstream_context *ums);

uint32_t UMstream_dec_step
 (uint32_t next, struct UMstream_context *ums);

void 
 UMsrandom(uint32_t seed);

uint32_t UMrandom_32();

uint64_t UMrandom_64();

void UMhash_init
 (struct UMhash_context *umh);
 
void UMhash_step
 (uint32_t next, struct UMhash_context *umh);
 
uint32_t UMhash_modify
 (uint32_t a, uint32_t b, struct UMhash_context *umh);

uint32_t UMhash_rotate
 (uint32_t a);
 
void UMhash_calc
 (uint32_t *data, size_t data_len, struct UMhash_context *umh);

void UMhash_calc_uchar
 (unsigned char *data, size_t data_len, struct UMhash_context *umh);

void block_8_to_32
 (uint8_t *bl8, uint32_t *bl32, size_t size);
 
void block_32_to_8
 (uint8_t *bl8, uint32_t *bl32, size_t size);
 
size_t fsize
 (FILE *in);
 
uint8_t * read_key_file
 (uint8_t *key_file_name);
 
void gen_key_file
 (char *key_file_name, uint32_t seed);

void UMcipher_calc_key_pair
 (char *password, uint32_t *key, uint32_t *sub_key);
 
uint8_t * UMcipher_token
 (uint32_t *token);
